/*
 * Copyright 2009 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */


package bw.weather;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import bw.util.*;

public class WeatherServlet extends HttpServlet
{
    public static final String ACTION = "action";
    public static final String LIST_LOCATIONS = "list";
    public static final String LOCATION_ID = "id";
    public static final String ADD_LOCATION = "add";
    public static final String DELETE_LOCATION = "delete";

    public void doGet ( HttpServletRequest req, HttpServletResponse res ) 
        throws ServletException, IOException
    {
        String action = req.getParameter( ACTION );
        String locId = req.getParameter( LOCATION_ID );
        String error = "";
        
        WeatherManager manager = WeatherManager.getInstance();

        res.setContentType( "text/xml" );

        PrintWriter out = res.getWriter();

        if ( ADD_LOCATION.equalsIgnoreCase( action ) )
        {
            WeatherLocationFactory factory = WeatherLocationFactory.getInstance();
            try
            {
                if ( manager.findLocationById( locId ) == null )
                {
                    WeatherLocation loc = factory.createWeatherLocation( locId );
                    manager.addLocation( loc );
                }
                else 
                {
                    //no duplicates
                    InvalidWeatherLocationException ex = new InvalidWeatherLocationException( locId,
                                                                                              "Weather location already exists" );
                    throw ex;
                }
            }
            catch ( InvalidWeatherLocationException ex )
            {
                //something
                error = ex.getMessage() + ": " + ex.getLocationId();
                //Log.getInstance().write( "Bad weather location: " + ex.getLocationId() );
            }
        }
        else if ( DELETE_LOCATION.equalsIgnoreCase( action ) )
        {
            WeatherLocation loc = manager.findLocationById( locId );
            manager.removeLocation( loc );
        }

        Vector locations = manager.getLocations();

        out.println( "<locations errorMessage=\"" + Util.XMLEscape( error ) + "\">" );

        for ( Enumeration e = locations.elements(); e.hasMoreElements(); )
        {
            WeatherLocation loc = (WeatherLocation) e.nextElement();
            out.println( loc.toXML() );
        }

        out.println( "</locations>" );

        out.close();
    }

}